package com.msun.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.msun.entity.PageResult;
import com.msun.entity.QueryPageBean;
import com.msun.entity.Result;
import com.msun.pojo.Menu;
import com.msun.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @RequestMapping("/findMenuPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return menuService.findMenuPage(queryPageBean);
    }
    @RequestMapping("/childMenuById")
    public Result childMenuById(Integer parentId){
        try {
            List<Menu> data = menuService.childMenuById(parentId);
            return new Result(true,"获取子菜单成功",data);
        }catch (Exception e){
            return new Result(false,"获取子菜单错误");
        }

    }
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menu.setParentMenuId(menu.getParentMenuId()==-1?null:menu.getParentMenuId());
            menuService.add(menu);
            return new Result(true, "添加菜单项成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加菜单项失败");
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        System.out.println();
        try {
            menuService.edit(menu);
            return new Result(true,"编辑菜单项成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑菜单项失败");
        }
    }
    @RequestMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id){
        System.out.println(id);
        try {
//            Integer id  = Integer.parseInt(ids);
            menuService.delete(id);
            return new Result(true,"删除菜单成功");
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除菜单项失败");
        }

    }
    //获取菜单用于展示数据
    @RequestMapping("/findMenuList2Show")
    public Result findMenuList2Show(){
//        // 获取保存在session中认证的上下文对象
//        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        // 这个可以直接获取登录的用户名
        String userName = SecurityContextHolder.getContext().getAuthentication().getName();
        try {
            List<Menu> data =  menuService.findMenuList2Show(userName);
            return new Result(true,"获取菜单项成功",data);
        }catch (Exception e){
            return new Result(false,"获取菜单项失败");
        }

    }
}
